package com.zlq.cn.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.annotations.UnLogin;
import com.zlq.cn.api.ApiMessage;
import com.zlq.cn.api.ApiParam;
import com.zlq.cn.config.Config;
import com.zlq.cn.config.FinderConfiguration;
import com.zlq.cn.entity.LocationLog;
import com.zlq.cn.entity.User;
import com.zlq.cn.entity.UserMsg;
import com.zlq.cn.service.un.LocationLogService;
import com.zlq.cn.service.un.UserMsgService;
import com.zlq.cn.service.un.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2020-06-13
 */
@RestController
@RequestMapping("/api/locationLog")
public class LocationLogController {
    @Autowired
    private LocationLogService locationLogService;
    @Resource
    private FinderConfiguration finderConfiguration;
    @Resource
    private UserService userService;
    @Autowired
    private UserMsgService userMsgService;

    @RequestMapping("/sync")
    @UnLogin
    public Object sync() throws Exception {
        LocationLogService.syncLocationLog();
        locationLogService.saveLocationLog();
        return null;
    }

    @RequestMapping("/locationLogList")
    public Object locationLogList(@RequestBody ApiParam apiParam, User loginUser,
                                  @NotNull(message = "查询用户不能为空") Integer userId,
                                  @NotNull(message = "开始时间不能为空") LocalDateTime startTime,
                                  @NotNull(message = "结束时间不能为空") LocalDateTime endTime) throws Exception {
        return locationLogService.lambdaQuery()
                .eq(LocationLog::getUserId, userId)
                .between(LocationLog::getCreateTime, startTime, endTime)
                .list();
    }

    @RequestMapping("/clearLocationLog")
    public Object clearLocationLog(@RequestBody ApiParam apiParam, User loginUser) throws Exception {
        if (userService.updateLocationResetCount(loginUser)) {
            locationLogService.lambdaUpdate()
                    .eq(LocationLog::getUserId, loginUser.getId())
                    .remove();
        }
        return ApiMessage.resultApiMessage("清除成功");
    }

    @RequestMapping("/userMsgList")
    public Object userMsgList(@RequestBody ApiParam apiParam, User loginUser,
                              Integer page,
                              Integer pageSize) {
        Page<UserMsg> queryPage = new Page<>();
        queryPage.setCurrent(page != null ? page : 1);
        queryPage.setSize(pageSize != null ? pageSize : Config.PAGE_SIZE);
        userMsgService.page(queryPage, new LambdaQueryWrapper<UserMsg>().eq(UserMsg::getToUserId, loginUser.getId()).orderByDesc(UserMsg::getCreateTime));
        if (queryPage.getRecords() != null) {
            for (UserMsg record : queryPage.getRecords()) {
                record.setMsgTitle(record.buildMsgTitle());
            }
        }
        return queryPage;
    }
}

